
import { deleteRequest, error, getRequest, ok, postRequest, putRequest } from "../utils/myAxios.ts";
import {Course, CourseAssignment, Res} from "../types";
import {getDepById} from "./DepartmentApi.ts";
import {getCourseById} from "./CourseApi.ts";

// 获取课程分配列表
export async function getCourseAssignmentList(departmentId?:string,courseId?:string): Promise<Res<Array<CourseAssignment>>> {
    let url = `/course-assignment?`;
    let urlparams = new URLSearchParams();
    if (departmentId){
        urlparams.set('departmentId', departmentId)
    }
    if (courseId){
        urlparams.set('courseId', courseId)
    }
    const list = await getRequest<Array<CourseAssignment>>(url + urlparams.toString());
    console.log('获取课程分配列表', list);
    if (list?.length >= 0) {
        //为每个分配查询课程名与院系名
        for (let i = 0; i < list.length; i++) {
            let course = await getCourseById(list[i].courseId);
            list[i].courseName = course.data.name;
        }
        return ok(list, 'ok');
    } else {
        return error([], "请求失败，请重试");
    }
}

// 分配课程给学生或教师
export async function assignCourse(departmentId:string, courseIds:string[]): Promise<Res<CourseAssignment>> {
    let isOk = true;
    //删除该学院下所有课程
    let res = await getCourseAssignmentList(departmentId);
    if (res.flag){
        for (let i = 0; i < res.data.length; i++){
            let deleteRes = await deleteCourseAssignment(res.data[i].id);
            if(!deleteRes.flag){
                isOk = false;
            }
        }
    }
    for (let i = 0; i < courseIds.length; i++){
        let addRes = await addCourseAssignment({departmentId:departmentId, courseId:courseIds[i]});
        if(!addRes.flag){
            isOk = false;
        }
    }

    return ok(res.data, '分配成功')
    // return isOk ? ok(res, '分配成功') : error({code:500,flag:false}, '分配失败');
}

//获取当前学院下的课程
export async function getCourseListByDepartmentId(departmentId:string): Res<Array<Course>> {
    let url = `/course-assignment?departmentId=${departmentId}`
    let resList = []
    const list = await getRequest<Array<CourseAssignment>>(url);
    console.log('获取课程分配列表', list);
    if (list?.length >= 0) {
        //为每个分配查询课程名与院系名
        for (let i = 0; i < list.length; i++) {
            let course = await getCourseById(list[i].courseId);
            //计算当前选课人数

            resList.push(course.data);
        }
        return ok(resList, 'ok');
    } else {
        return error([], "请求失败，请重试");
    }
}

// 添加课程分配信息
export async function addCourseAssignment(courseAssignment: CourseAssignment): Promise<Res<CourseAssignment>> {
    const r = await postRequest<CourseAssignment>(`/course-assignment`, courseAssignment);
    console.log('添加课程分配res：', r);
    if (r.id) {
        return ok(r, '添加成功');
    } else {
        return error(r);
    }
}

// 更新课程分配信息
export async function updateCourseAssignment(courseAssignment: CourseAssignment): Promise<Res<CourseAssignment>> {
    const r = await putRequest<CourseAssignment>(`/course-assignment/${courseAssignment.id}`, courseAssignment);
    console.log('更新课程分配res：', r);
    if (r.id) {
        return ok(r, '更新成功');
    } else {
        return error(r);
    }
}

// 删除课程分配
export async function deleteCourseAssignment(id: string): Promise<Res<CourseAssignment>> {
    const r = await deleteRequest<CourseAssignment>(`/course-assignment/${id}`);
    console.log('删除课程分配res：', r);
    if (r.id) {
        return ok(r, '删除成功');
    } else {
        return error(r);
    }
}

// 查询单个课程分配详情
export async function getCourseAssignmentById(id: string): Promise<Res<CourseAssignment>> {
    const r = await getRequest<CourseAssignment>(`/course-assignment/${id}`);
    console.log('查询课程分配res：', r);
    if (r.id) {
        return ok(r, '查询成功');
    } else {
        return error(r);
    }
}

